package com.liao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liao.entity.Book;
import com.liao.entity.BookCategory;
import com.liao.mapper.BookCategoryMapper;
import com.liao.mapper.BookMapper;
import com.liao.service.BookCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 图书分类服务实现类
 */
@Service
public class BookCategoryServiceImpl extends ServiceImpl<BookCategoryMapper, BookCategory> implements BookCategoryService {

    @Resource
    private BookMapper bookMapper;

    /**
     * 分页查询图书分类列表
     *
     * @param page 分页参数
     * @param name 分类名称（可选）
     * @return 分页图书分类列表
     */
    @Override
    public Page<BookCategory> getCategoryPage(Page<BookCategory> page, String name) {
        LambdaQueryWrapper<BookCategory> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        queryWrapper.like(StringUtils.hasText(name), BookCategory::getName, name);
        
        // 查询并返回结果
        return this.page(page, queryWrapper);
    }

    /**
     * 添加图书分类
     *
     * @param bookCategory 图书分类信息
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCategory(BookCategory bookCategory) {
        // 检查分类名称是否已存在
        LambdaQueryWrapper<BookCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookCategory::getName, bookCategory.getName());
        if (this.count(queryWrapper) > 0) {
            throw new RuntimeException("分类名称已存在");
        }
        
        // 保存分类
        return this.save(bookCategory);
    }

    /**
     * 修改图书分类
     *
     * @param bookCategory 图书分类信息
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCategory(BookCategory bookCategory) {
        // 检查分类是否存在
        BookCategory existCategory = this.getById(bookCategory.getId());
        if (existCategory == null) {
            throw new RuntimeException("分类不存在");
        }
        
        // 检查分类名称是否已存在（排除自身）
        LambdaQueryWrapper<BookCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookCategory::getName, bookCategory.getName())
                .ne(BookCategory::getId, bookCategory.getId());
        if (this.count(queryWrapper) > 0) {
            throw new RuntimeException("分类名称已存在");
        }
        
        // 更新分类
        return this.updateById(bookCategory);
    }

    /**
     * 删除图书分类
     *
     * @param id 分类ID
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCategory(Long id) {
        // 检查分类是否存在
        BookCategory category = this.getById(id);
        if (category == null) {
            throw new RuntimeException("分类不存在");
        }
        
        // 检查分类下是否有图书
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Book::getCategoryId, id);
        if (bookMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("该分类下存在图书，无法删除");
        }
        
        // 删除分类
        return this.removeById(id);
    }

    /**
     * 根据分类ID获取分类名称
     *
     * @param id 分类ID
     * @return 分类名称
     */
    @Override
    public String getCategoryNameById(Long id) {
        BookCategory category = this.getById(id);
        return category != null ? category.getName() : null;
    }
}